import * as session from 'express-session';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { AppModule } from './app.module';
import { TransformInterceptor } from './core/interceptor/transform.interceptor';
import { HttpExceptionFilter } from './core/filter/http-exception.filter';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  // 自动验证
  app.useGlobalPipes(new ValidationPipe());
  // 配置跨域
  app.enableCors({
    origin: ['http://localhost:5556', 'http://localhost:3333'],
    allowedHeaders: ['Authorization', 'content-type', 'Souche-Inc-Security-Token', 'Souche-Security-Token', 'souche-security-token-inc', 'hideLoading'],
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
  });
  // session中间件
  app.use(
    session({
      secret: 'my-secret',
      resave: false,
      saveUninitialized: false,
    }),
  );
  // 集成swagger
  const options = new DocumentBuilder()
    .setTitle('接口文档')
    .setDescription('Nest服务器应用接口文档')
    .setVersion('1.0')
    .addBearerAuth()
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api', app, document);
  // 注册全局过滤器
  app.useGlobalFilters(new HttpExceptionFilter());
  // 全局注册拦截器
  app.useGlobalInterceptors(new TransformInterceptor());
  await app.listen(3000);
}
bootstrap();
